<?php
// use the dPFramework to have easy database operations (store, delete etc.) by using its ObjectOrientedDesign
// therefore we have to create a child class for the module einstein

// a class named (like this) in the form: module/module.class.php is automatically loaded by the dPFramework

/**
 *	@package dotProject
 *	@subpackage modules
 *	@version $Revision: 1.0 $
*/

// include the powerful parent class that we want to extend for einstein
require_once( $AppUI->getSystemClass ('dp' ) );		// use the dPFramework for easy inclusion of this class here

/**
 * The Classification Class
 */
class CMdClassification extends CDpObject {
	// link variables to the einstein object (according to the existing columns in the database table einstein)
	var $cls_id = NULL;	//use NULL for a NEW object, so the database automatically assigns an unique id by 'NOT NULL'-functionality
	var $cls_code = NULL;
	var $cls_name = NULL;
	//var $cls_type = NULL;
	/* sql update
	 * ALTER TABLE `classifications` ADD `cls_active` INT( 4 ) NOT NULL DEFAULT '1';
	 */
	var $cls_active = 1;
	var $cls_vat_exemption = 0;

	// the constructor of the CEinstein class, always combined with the table name and the unique key of the table
	function CMdClassification() {
		$joins = array(
			array('label'=>'Customer',name=>'companies', 'idfield'=>'company_id', 'joinfield'=>'company_classification'),
			array('label'=>'Suppliers',name=>'suppliers', 'idfield'=>'sup_id', 'joinfield'=>'sup_classification'),
		);
		$this->CDpObject( 'classifications', 'cls_id', 'cls_code', $joins, 'masterdata');
	}

	function check() {
		if (!isset($this->_tbl_unique) || $this->_tbl_unique == NULL) {
			return NULL;
		}
		$q = new DBQuery();
		$q->addTable("$this->_tbl");
		$q->addWhere("$this->_tbl_key != $this->cls_id");
		$q->addWhere("$this->_tbl_unique = '$this->cls_code'");
		$result = $q->loadResult();
		if (isset($result) && $result != '') {
			return 'Duplicate classifications code: ' . $this->cls_code;
		}
		return NULL;
	}
	/*
 	function getTypeName(){
		//$sql = "select status_name from md_status where status_id = '" . $this->unit_status . "'";
		$q  = new DBQuery;
		$q->addTable('md_type');
		$q->addQuery('name');
		$q->addWhere('type_id = '.$this->cls_type);
		$sql = $q->prepare();
		$q->clear();
		$type_name = db_loadResult( $sql );
		return $type_name;
 	}

 	function getTypeList(){
 		$result = array('type_id' => 0, 'type_name' => '');
		$q  = new DBQuery;
		$q->addTable('md_type');
		$q->addQuery('type_id, name');
		$q->addWhere('type =\'Classification\'');
		$sql = $q->prepare();
		$q->clear();
		//db_loadHash($sql, $result);
		return db_loadHashList($sql);
 	}
 	*/

	function loadAllClassification($active = null){
		$q = new DBQuery;
		$q->addTable('classifications');
		//$q->addJoin('md_type', 'type', 'type_id = cls_type');
		//$q->addQuery('classifications.*, type.name as type_name');
		$q->addQuery('*');
		if($active != null || $active !=''){
			$q->addWhere('cls_active = '.$active);
		}
		$psql = $q->prepare();
		$q->clear();
		return db_loadList($psql);

	}
}
function getClassificationList($require = true){
	$q  = new DBQuery;
	$q->addTable('classifications');
	$q->addQuery('cls_id, cls_name');
	//$q->addJoin('md_type', 't', 't.type_id = cls_type');
	$q->addWhere('cls_active = 1');
	$sql = $q->prepare();
	$q->clear();
	
	$results = db_loadHashList($sql);
	if (!$require) {
		return arrayMerge(array('' => ''), $results);
	}
	return $results;
}
?>
